home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 247_01 / manual1.doc < prev   
Text File  |  1989-04-17  |  45KB  |  1,651 lines

  1.     M.I.R.A.C.L. - Multiprecision Integer and Rational Arithmetic C Library    _______________________________________________________________________
  2.  
  3.                                  M. Scott
  4.  
  5.  
  6. ABSTRACT:
  7.  
  8.  
  9.  
  10.       A portable C library is described  which  implements  multi-precision
  11.  
  12.       integer and rational data-types, and provides the routines to perform
  13.  
  14.       basic  arithmetic  on them.  Certain useful number-theoretic routines
  15.  
  16.       are also included in the package.
  17.  
  18.  
  19.  
  20. 1. INTRODUCTION:
  21.  
  22.  
  23.  
  24.       Conventional computer  arithmetic  facilities  as  provided  by  most
  25.  
  26.       computer language compilers usually provide one or two floating-point
  27.  
  28.       data  types  (e.g.  single and double precision) to represent all the
  29.  
  30.       real numbers,  together with one or more integer types  to  represent
  31.  
  32.       whole  numbers.  These built-in data-types are closely related to the
  33.  
  34.       underlying computer architecture,  which is sensibly designed to work
  35.  
  36.       quickly with large amounts of small numbers,  rather than slowly with
  37.  
  38.       small amounts of large numbers (given  a  fixed  memory  allocation).
  39.  
  40.       Floating-point allows a relatively small binary number (e.g. 32 bits)
  41.  
  42.       to  represent  real numbers to an adequate precision (e.g.  7 decimal
  43.  
  44.       places) over a large dynamic range.  Integer types allow small  whole
  45.  
  46.       numbers to be represented directly by their binary equivalent,  or in
  47.  
  48.       2's complement  form  if  negative.  Nevertheless  this  conventional
  49.  
  50.       approach to computer arithmetic has several disadvantages.
  51.  
  52.  
  53.  
  54.       (1)   Floating-point  and  Integer  data-types  are  representationly
  55.  
  56.       incompatible.  Note that the set of integers, although infinite, is a
  57.  
  58.  
  59.  
  60.                                       1                                       
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.       subset of the rationals (i.e.  fractions),  which is in turn a subset
  68.  
  69.       of the reals.  Thus every integer has  an  equivalent  floating-point
  70.  
  71.       representation.  Unfortunately  these  two  representations  will  in
  72.  
  73.       general be different.  For example a small positive whole number will
  74.  
  75.       be  represented  by  its  binary  equivalent  as  an integer,  and as
  76.  
  77.       separated mantissa and exponent as a floating-point. This implies the
  78.  
  79.       need for conversion routines, to convert from one form to the other.
  80.  
  81.  
  82.  
  83.       (2) Most rational numbers can not be expressed  exactly  (e.g.  1/3).
  84.  
  85.       Indeed  the  floating-point  system  can  only  express exactly those
  86.  
  87.       rationals whose denominators are multiples  of  the  factors  of  the
  88.  
  89.       underlying  radix.  For  example our familiar decimal system can only
  90.  
  91.       represent exactly  those  rational  numbers  whose  denominators  are
  92.  
  93.       multiples of 2 and 5; 1/20 is 0.05 exactly, 1/21 is .0476190476190...
  94.  
  95.  
  96.  
  97.       (3) Rounding in floating-point is  base-dependant  and  a  source  of
  98.  
  99.       obscure errors.
  100.  
  101.  
  102.  
  103.       (4)  The  fact that the size of integer and floating-point data types
  104.  
  105.       are dictated by the computer architecture,  defeats  the  efforts  of
  106.  
  107.       language designers to keep their languages portable.
  108.  
  109.  
  110.  
  111.       (5)  Numbers  can  only  be  represented to a fixed machine-dependent
  112.  
  113.       precision. In many applications this can be a crippling disadvantage,
  114.  
  115.       for example in the new and growing field of Public-Key cryptography.
  116.  
  117.  
  118.  
  119.       (6) Base-dependent phenomena cannot easily be studied. For example it
  120.  
  121.       would be difficult to access a particular digit of a decimal  number,
  122.  
  123.       as  represented  by  a  traditional  integer  data-type.
  124.  
  125.  
  126.                                       2                                       
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.       Herein is described a set of standard  C  routines  which  manipulate
  136.  
  137.       multi-precision  rational  numbers  directly,   with  multi-precision
  138.  
  139.       integers as a compatible subset. Approximate real arithmetic can also
  140.  
  141.       be performed.
  142.  
  143.  
  144.  
  145.  
  146.  
  147. 2. NUMBER REPRESENTATION
  148.  
  149.  
  150.  
  151.       The two new data-types are called "big" and "flash".  The  former  is
  152.  
  153.       used  to  store  multiprecision  integers,   and  the  latter  stores
  154.  
  155.       multiprecision fractions as numerator and denominator  in  "floating-
  156.  
  157.       slash" form.  Both take the form of a fixed length array of integers,
  158.  
  159.       with sign and length information encoded in the  first  element,  and
  160.  
  161.       the  data  itself  in  subsequent  elements.  Both  new  types can be
  162.  
  163.       introduced into the syntax of the 'C' language by the 'C'  statements
  164.  
  165.                             typedef int* big;
  166.                             typedef int* flash;
  167.  
  168.       Now  'big' and 'flash' variables can be declared just like any built-
  169.  
  170.       in data type, e.g.
  171.  
  172.  
  173.  
  174.                              big x,y[10],z[10][10];
  175.  
  176.  
  177.  
  178.       Note that the user of these data-types is  not  concerned  with  this
  179.  
  180.       internal representation; the library routines allow "big" and "flash"
  181.  
  182.       numbers to be manipulated directly.
  183.  
  184.  
  185.  
  186.       The structure of "big" and "flash" numbers is illustrated in figure (1)
  187.  
  188.  
  189.  
  190.  
  191.  
  192.                                       3                                       
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.         x[0]    x[1]   x[2]                x[n]                          x[max]
  200.       ┌─────┐ ┌─────┐ ┌─────┐             ┌─────┐ ┌─────┐                ┌─────┐
  201.       │s 0 n│ │ LSW │ │     │ ..........  │ MSW │ │  0  │ .............. │  0  │
  202.       └─────┘ └─────┘ └─────┘             └─────┘ └─────┘                └─────┘
  203.  
  204.  
  205.         x[0]   x[1]    x[2]        x[n]   x[n+1]   x[n+2]     x[n+d]     x[max]
  206.       ┌─────┐ ┌─────┐ ┌─────┐     ┌─────┐ ┌─────┐ ┌─────┐     ┌─────┐    ┌─────┐
  207.       │s d n│ │ LSW │ │     │ ... │ MSW │ │ LSW │ │     │ ... │ MSW │ .. │  0  │
  208.       └─────┘ └─────┘ └─────┘     └─────┘ └─────┘ └─────┘     └─────┘    └─────┘
  209.               <------- numerator ------> / <------ denominator ----->
  210.  
  211.  
  212.        Figure (1):  Structure of "big" and "flash" data-types,  where s  is
  213.                    the  sign of the number,  n and d are the lengths of the
  214.                    numerator and denominator respectively,  and LSW and MSW
  215.                    mean  'Least  significant  word'  and  'Most significant
  216.                    word'
  217.  
  218.  
  219.  
  220.       These   structures   combine   ease   of  use  with  representational
  221.  
  222.       efficiency.  A denominator of length zero (d=0),  implies  an  actual
  223.  
  224.       denominator  of one;  and similarily a numerator of length zero (n=0)
  225.  
  226.       implies  a  numerator of one.  Zero itself is uniquely defined as the
  227.  
  228.       number whose first element is zero (i.e. n=d=0).
  229.  
  230.  
  231.  
  232.       Note that the slash in the  'flash'  data-type  is  not  in  a fixed
  233.  
  234.       position,    and  may  "float"   depending  on the relative  size  of
  235.  
  236.       numerator and denominator.
  237.  
  238.  
  239.  
  240.       A "flash" number is manipulated by  splitting  it  up  into  separate
  241.  
  242.       "big"  numerator  and  denominator  components.  A  "big"  number  is
  243.  
  244.       manipulated by extracting and operating  on  each  of  its  component
  245.  
  246.       integer elements.  To avoid possible confusion with the internal sign
  247.  
  248.       bit,  the  numbers  in each element are limited to a somewhat smaller
  249.  
  250.       range than that of the full word-length, e.g. 0 to 16383  (= 2^14 -1)
  251.  
  252.       on a 16-bit micro-computer.
  253.  
  254.  
  255.  
  256.  
  257.  
  258.                                       4                                       
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.       When the system is initialised the user specifies the fixed number of
  266.  
  267.       words (or bytes) to be assigned to all "big"  or  "flash"  variables,
  268.  
  269.       and the number base to be used. Any base can be used, up to a maximum
  270.  
  271.       MAXBASE which is dependant on the wordlength of the computer used. If
  272.  
  273.       requested  to  use  a  small  base  b,  the system will,  for optimal
  274.  
  275.       efficiency,  actually use base bⁿ ,  where n is the  largest  integer
  276.  
  277.       such that bⁿ fits in a single computer word.
  278.  
  279.  
  280.  
  281.       The   encoding  of  the  sign  and  numerator  and  denominator  size
  282.  
  283.